package com.work.csms.mapper;

import com.work.csms.dao.Station;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface StationMapper {

    @Select({"<script>" +
            "SELECT * FROM station " +
            "where 1 = 1" +
            "<if test=\"code != null and code != '' \"> " +
            " AND name like concat('%',#{code},'%')" +
            "</if>" +
            " limit #{offset} , #{size}" +
            "</script>"})
    List<Station> page(String code, Integer offset, Integer size);

    @Select({"<script>" +
            "SELECT count(0) FROM station " +
            "where 1 = 1" +
            "<if test=\"code != null and code != '' \"> " +
            " AND name like concat('%',#{code},'%')" +
            "</if>" +
            "</script>"})
    Integer count(String code);

    @Select({"<script>" +
            "SELECT * FROM station " +
            "where 1 = 1" +
            "<if test=\"statu != null and statu != '' \"> " +
            " AND statu = #{statu}" +
            "</if>" +
            "</script>"})
    List<Station> list(Station station);

    @Select("SELECT * FROM Station WHERE id=#{id}")
    Station getStationById(Integer id);

    @Update({"<script>" +
            "update station set " +
            "<if test=\"name != null and name != '' \"> " +
            " `name` = #{name}," +
            "</if>" +
            "<if test=\"code != null and code != '' \"> " +
            " code = #{code}," +
            "</if>" +
            "<if test=\"position != null and position != '' \"> " +
            " `position` = #{position}," +
            "</if>" +
            "<if test=\"remark != null and remark != '' \"> " +
            " remark = #{remark}," +
            "</if>" +
            "<if test=\"statu != null and statu != '' \"> " +
            " statu = #{statu}," +
            "</if>" +
            " id = #{id}" +
            " where id = #{id}" +
            "</script>"})
    void update(Station station);

    @Insert("INSERT INTO station(NAME, code, position, remark, statu) VALUES " +
            "(#{station.name}, #{station.code}, #{station.position}, #{station.remark}, #{station.statu})")
    void save(@Param("station")Station station);

    @Delete("delete from station where id = #{id} ")
    void deleted(Integer id);
}
